package com.gticket.customerbackend.mapper;

import com.gticket.customerbackend.pojo.CinemaActualSeats;
import com.gticket.customerbackend.pojo.CinemaCode;
import com.gticket.customerbackend.pojo.Movie;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author 李浩楠
 * @date 2019/11/23 下午2:25
 */
@Repository
public interface MovieScheduleMapper {

    @Select("Select * from ${cinemacode.db}.movies")
    public List<Movie> getAllMovies(@Param("cinemacode") CinemaCode cinemaCode);

    @Insert("Insert into ${cinemacode.db}.movies(" +
            "movie_name,movie_englishname,movie_act,movie_descr,movie_detail," +
            "movie_type,movie_country,movie_time,movie_date,movie_price) VALUES(" +
            "#{movie_name},#{movie_englishname},#{movie_act},#{movie_descr},#{movie_detail}," +
            "#{movie_type},#{movie_country},#{movie_time},#{movie_date},#{movie_price})")
    public boolean insertNewMovie(@Param("cinemacode") CinemaCode cinemaCode,
                                  @Param("movie_name") String movie_name,
                                  @Param("movie_englishname") String movie_englishname,
                                  @Param("movie_act") String movie_act,
                                  @Param("movie_descr") String movie_descr,
                                  @Param("movie_detail") String movie_detail,
                                  @Param("movie_type") String movie_type,
                                  @Param("movie_country") String movie_country,
                                  @Param("movie_time") int movie_time,
                                  @Param("movie_date") String movie_date,
                                  @Param("movie_price") BigDecimal movie_price);

    // 插入根据日期找电影
    @Select("SELECT filmschedule.movie_id,filmschedule.start_time,filmschedule.end_time FROM ${cinemacode.db}.filmschedule \n" +
            "WHERE filmschedule.cinema_id = #{cinema_id} \n" +
            "AND filmschedule.ch_id = #{ch_id} \n" +
            "AND filmschedule.fs_date = #{fs_date} \n" +
            "AND filmschedule.flag = 0")
    public List<Movie> getScheduledMovieByDate(@Param("cinemacode") CinemaCode cinemaCode,
                                               @Param("cinema_id") int cinema_id,
                                               @Param("ch_id") int ch_id,
                                               @Param("fs_date") String fs_date);

    // 根据电影id查找电影
    @Select("Select * from ${cinemacode.db}.movies where movie_id = #{movie_id}")
    public Movie getMovieByMovieId(@Param("cinemacode") CinemaCode cinemaCode,
                                   @Param("movie_id") int movie_id);

    // 查找刚插入的电影的id
    @Select("SELECT movie_id FROM ${cinemacode.db}.movies WHERE movie_name = #{movie_name}")
    public int getMovieIdByName(@Param("cinemacode") CinemaCode cinemaCode,
                                @Param("movie_name") String movie_name);

    // 查找座位表赋值给实际表的pojo
    @Select("Select * from ${cinemacode.db}.originalseat where cinema_id = #{cinema_id} AND ch_id = #{ch_id}")
    public CinemaActualSeats getTempSeats(@Param("cinemacode") CinemaCode cinemaCode,
                                          @Param("cinema_id") int cinema_id,
                                          @Param("ch_id") int ch_id);

    // 从座位temp中取值赋给as表,首先创建表
    @Insert("Insert into ${cinemacode.db}.actualseat(cinema_id,ch_id,row_1,row_2,row_3,row_4,row_5,row_6," +
            "row_7,row_8,row_9,row_10,row_11,row_12,row_13,row_14,row_15,stock) " +
            "VALUES(#{cinema_id},#{ch_id},#{row_1},#{row_2},#{row_3},#{row_4},#{row_5},#{row_6},#{row_7}," +
            "#{row_8},#{row_9},#{row_10},#{row_11},#{row_12},#{row_13},#{row_14},#{row_15},#{stock})")
    public boolean createActualSeats(@Param("cinemacode") CinemaCode cinemaCode,
                                     @Param("cinema_id") int cinema_id,
                                     @Param("ch_id") int ch_id,
                                     @Param("row_1") String row_1,
                                     @Param("row_2") String row_2,
                                     @Param("row_3") String row_3,
                                     @Param("row_4") String row_4,
                                     @Param("row_5") String row_5,
                                     @Param("row_6") String row_6,
                                     @Param("row_7") String row_7,
                                     @Param("row_8") String row_8,
                                     @Param("row_9") String row_9,
                                     @Param("row_10") String row_10,
                                     @Param("row_11") String row_11,
                                     @Param("row_12") String row_12,
                                     @Param("row_13") String row_13,
                                     @Param("row_14") String row_14,
                                     @Param("row_15") String row_15,
                                     @Param("stock") int stock);

    @Select("select @@identity")
    public int last_id();

    @Insert("Insert into ${cinemacode.db}.filmschedule(cinema_id,ch_id,movie_id," +
            "as_id,start_time,end_time,fs_date,movie_version,movie_realprice) VALUES(#{cinema_id},#{ch_id},#{movie_id},#{as_id}," +
            "#{start_time},#{end_time},#{fs_date},#{movie_version},#{movie_realprice})")
    public boolean createNewMovieSchedule(@Param("cinemacode") CinemaCode cinemaCode,
                                          @Param("cinema_id") int cinema_id,
                                          @Param("ch_id") int ch_id,
                                          @Param("movie_id") int movie_id,
                                          @Param("as_id") int as_id,
                                          @Param("start_time") String start_time,
                                          @Param("end_time") String end_time,
                                          @Param("fs_date") String fs_date,
                                          @Param("movie_version")String movie_version,
                                          @Param("movie_realprice")BigDecimal movie_realprice);

    // 修改排片表的flag,让表过期.
    @Update("Update ${cinemacode.db}.filmschedule SET flag = 1 where fs_id = #{fs_id}")
    public void updateMovieScheduleFlag(@Param("cinemacode") CinemaCode cinemaCode,
                                        @Param("fs_id") int fs_id);
}
